/**
 * 伪造 auth 请求
 */

import { randomString, getTimestamp } from './util'
import CryptoJS from 'crypto-js'
import { getHeaderAES } from './AESCrypt'
import axios from 'axios'
import { loginApi } from '../api'

// const baseUrl = 'http://lumihui.h5.emhome.cn'

export function authRequest () {
  const params = generateSignature()
  axios.post(`/token/oauth2/authorize`, params)
    .then(res => {
      console.log(res)
      localStorage.setItem('accessToken', res.data.access_token)
    })
    .catch(e => {
      console.log(e)
    })
}

/**
 * 生成签名字符串
 * @param {*} code
 */
function generateSignature () {
  const nonce = randomString(32)
  const appid = '2018101412'
  let platform = 'ios'
  const model = 'iPhone8 Plus'

  let params = {
    appid: appid,
    timestamp: getTimestamp(),
    os: platform,
    model: model,
    build: 1,
    install: '0',
    sn: 'lixlixlixlixlixlixlixlixlixlix'
  }
  let str = ''
  for (let key of Object.keys(params).sort()) {
    str += params[key]
  }
  let secret = 'Ghp350V9UHqrzGgc6hqvYxg0rqLsyFXR'
  let sign = CryptoJS.SHA256(secret + str + nonce).toString(CryptoJS.enc.Hex)

  params['nonce'] = nonce
  params['sign'] = sign
  return params
}

/**
 * 对请求头的token进行AES加密
 * @param {*} data
 */
export function cryptoToken (data) {
  return getHeaderAES(data)
}

export function login () {
  loginApi({
    username: '13188877109',
    password: 'lx123456'
  })
    .then(res => {
      console.log(res)
    })
    .catch(e => {
      console.log(e)
    })
}
